package erp.server.message.service.impl;

import erp.server.message.enums.MessageStatus;
import erp.server.message.mapper.*;
import erp.server.message.pojo.dto.BoxOperationDTO;
import erp.server.message.pojo.dto.BoxSearchDTO;
import erp.server.message.pojo.dto.MessageCopyDTO;
import erp.server.message.pojo.entity.*;
import erp.server.message.pojo.vo.MessageListVO;
import erp.server.message.service.BoxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class BoxServiceImpl implements BoxService {
    @Autowired
    DraftsMapper draftsMapper;
    @Autowired
    InboxMapper inboxMapper;
    @Autowired
    OutboxMapper outboxMapper;
    @Autowired
    CollectBoxMapper collectBoxMapper;
    @Autowired
    OrgBoxMapper orgBoxMapper;
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private MessageContentMapper messageContentMapper;



    /**
     * 获取信箱中的消息列表
     */
    @Override
    public List<MessageListVO> selectMessages(BoxSearchDTO boxSearchDTO) {
        List<MessageListVO> messages = new ArrayList<>();
        switch (boxSearchDTO.getBoxType()){
            case DRAFT:
                messages = draftsMapper.selectMessages(boxSearchDTO);
                break;
            case INBOX:
                log.debug("查询收件表");
                messages = inboxMapper.selectMessages(boxSearchDTO);
                break;
            case OUTBOX:
                messages = outboxMapper.selectMessages(boxSearchDTO);
                break;
            case COLLECTBOX:
                messages = collectBoxMapper.selectMessages(boxSearchDTO);
                break;
            case ORGBOX:
                messages = orgBoxMapper.selectMessages(boxSearchDTO);
                break;
        }
        return messages;
    }


    /**
     * 删除信箱中的指定消息
     */
    @Override
    public void deleteMessage(BoxOperationDTO boxOperationDTO) {
        // 删除消息和消息内容
        messageMapper.deleteById(boxOperationDTO.getMessageId());
        messageContentMapper.deleteById(boxOperationDTO.getMessageId());
        // 删除信箱记录
        switch (boxOperationDTO.getBoxType()){
            case DRAFT:
                draftsMapper.deleteById(boxOperationDTO.getMessageId());
                break;
            case INBOX:
                inboxMapper.deleteById(boxOperationDTO.getMessageId());
                break;
            case OUTBOX:
                outboxMapper.deleteById(boxOperationDTO.getMessageId());
                break;
            case COLLECTBOX:
                collectBoxMapper.deleteById(boxOperationDTO.getMessageId());
                break;
            case ORGBOX:
                orgBoxMapper.deleteById(boxOperationDTO.getMessageId());
                break;
        }
    }


    /**
     * 复制消息至指定信箱
     * @param messageCopyDTO 复制消息到指定信箱
     */
    @Override
    public void copyMessage(MessageCopyDTO messageCopyDTO) {
        /*
         取出原消息和对应内容并复制
         在指定信箱插入新消息
         */
        Message message = messageMapper.selectById(messageCopyDTO.getId());
        MessageContent messageContent = messageContentMapper.selectById(message.getId());
        message.setId(null);
        message.setCreateTime(new Date());
        message.setUpdateTime(new Date());
        message.setStatus(MessageStatus.FINISHED);
        messageMapper.insert(message);
        messageContent.setMessageId(message.getId());
        messageContentMapper.insert(messageContent);
        // 插入指定信箱
        switch (messageCopyDTO.getDesBox()){
            case DRAFT:
                Draft draft = new Draft();
                draft.setMessageId(message.getId());
                draft.setUserId(message.getUserId());
                draft.setOrgId(message.getOrgId());
                draft.setType(message.getType());
                draftsMapper.insert(draft);
                break;
            case INBOX:
                Inbox inbox = new Inbox();
                inbox.setMessageId(message.getId());
                inbox.setUserId(message.getDesUserId());
                inbox.setOrgId(message.getDesOrgId());
                inbox.setType(message.getType());
                inboxMapper.insert(inbox);
                break;
            case OUTBOX:
                Outbox outbox = new Outbox();
                outbox.setMessageId(message.getId());
                outbox.setUserId(message.getUserId());
                outbox.setOrgId(message.getOrgId());
                outbox.setType(message.getType());
                outboxMapper.insert(outbox);
                break;
            case COLLECTBOX:
                CollectBox collectBox = new CollectBox();
                collectBox.setMessageId(message.getId());
                collectBox.setUserId(message.getUserId());
                collectBox.setOrgId(message.getOrgId());
                collectBox.setType(message.getType());
                collectBoxMapper.insert(collectBox);
                break;
            case ORGBOX:
                OrgBox orgBox = new OrgBox();
                orgBox.setMessageId(message.getId());
                orgBox.setOrgId(message.getOrgId());
                orgBox.setType(message.getType());
                orgBoxMapper.insert(orgBox);
                break;
        }
    }



}

































